package android.slc.fast.vd.list

import android.slc.adapter.PageModel
import android.slc.box.core.property.DataChangeListener
import javafx.scene.control.Pagination
import javafx.scene.layout.Region

/**
 * @author slc
 * @date 2024/12/5
 **/
open class PaginationViewHelper {

    lateinit var vmSub: BaseListPageVmSubByFx<*>
        private set

    lateinit var pagination: Pagination
        private set

    protected var shouldRefresh = true

    open fun getPaginationId(): String {
        return "#pagination"
    }

    open fun initPagination(contentView: Region, vmSub: BaseListPageVmSubByFx<*>) {
        this.vmSub = vmSub
        this.pagination = contentView.lookup(getPaginationId()) as Pagination
        this.pagination.currentPageIndexProperty().addListener(DataChangeListener {
            onSelectPageIndex(it as Int + this.vmSub.loadMoreDataFormatByFx.baseOffset)
        })
    }

    protected open fun onSelectPageIndex(index: Int) {
        if (this.shouldRefresh) {
            this.shouldRefresh = false
            this.vmSub.refresh()
        }
        vmSub.loadMore(index, true)
    }

    open fun loadMoreComplete(pageModel: PageModel<*>) {
        this.pagination.pageCount = if (pageModel.pages == 0) 1 else pageModel.pages
        this.pagination.currentPageIndex = vmSub.loadMoreDataFormatByFx.offset - 1
    }

}